package it.mwt.uivaq.utility;

import it.univaq.mwt.model.entity.Gift;
import it.univaq.mwt.model.entity.News;

import java.io.StringWriter;
import java.util.List;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;

public class RssGenerator {

	public Document getFeed(List<News> element, String t, String l, String des, String lan) {

		String feed = null;
		
		Document rss = null;
       
        try {
            rss = ((DocumentBuilderFactory.newInstance()).newDocumentBuilder()).newDocument();
        } catch (ParserConfigurationException pce) {
            System.out.println("Cannot create DOM tree");
            pce.printStackTrace();
        }
        
        Element root=rss.createElement("rss");
        root.setAttribute("version", "2.0");
        rss.appendChild(root);
        
        Element channel=rss.createElement("channel");
        root.appendChild(channel);
        
        Element title=rss.createElement("title");
        title.setTextContent(t);
        channel.appendChild(title);
        
        Element link=rss.createElement("link");
        link.setTextContent(l);
        channel.appendChild(link);
        
        Element description=rss.createElement("description");
        description.setTextContent(des);
        channel.appendChild(description);
        
        Element language=rss.createElement("language");
        language.setTextContent(lan);
        channel.appendChild(language);
        
        			
		for(int i=0; i<element.size();i++){
			
			 Node item=rss.createElement("item");
			 channel.appendChild(item);
		     
		     Node itTitle=rss.createElement("title");
		     itTitle.setTextContent(element.get(i).getTitle());
		     item.appendChild(itTitle);
		     
		     Node itLink=rss.createElement("link");
		     itLink.setTextContent(element.get(i).getLink());
		     item.appendChild(itLink);
		     
		     Node guid=rss.createElement("guid");
		     guid.setTextContent(element.get(i).getLink());
		     item.appendChild(guid);
		     
		     Node date=rss.createElement("pubDate");
		     date.setTextContent(element.get(i).getCreation_date().toGMTString());
		     item.appendChild(date);
		     
		     Node itDes=rss.createElement("description");
		     itDes.setTextContent(element.get(i).getDescription());
		     item.appendChild(itDes);
		     
		}
        
		return rss;
	}
	
	public Document getGiftFeed(List<Gift> element, String t, String l, String des, String lan) {

		String feed = null;
		
		Document rss = null;
       
        try {
            rss = ((DocumentBuilderFactory.newInstance()).newDocumentBuilder()).newDocument();
        } catch (ParserConfigurationException pce) {
            System.out.println("Cannot create DOM tree");
            pce.printStackTrace();
        }
        
        Element root=rss.createElement("rss");
        root.setAttribute("version", "2.0");
        rss.appendChild(root);
        
        Element channel=rss.createElement("channel");
        root.appendChild(channel);
        
        Element title=rss.createElement("title");
        title.setTextContent(t);
        channel.appendChild(title);
        
        Element link=rss.createElement("link");
        link.setTextContent(l);
        channel.appendChild(link);
        
        Element description=rss.createElement("description");
        description.setTextContent(des);
        channel.appendChild(description);
        
        Element language=rss.createElement("language");
        language.setTextContent(lan);
        channel.appendChild(language);
        
        			
		for(int i=0; i<element.size();i++){
			
			 Node item=rss.createElement("item");
			 channel.appendChild(item);
		     
		     Node itTitle=rss.createElement("title");
		     itTitle.setTextContent(element.get(i).getName());
		     item.appendChild(itTitle);
		     
		     Node itLink=rss.createElement("link");
		     itLink.setTextContent(element.get(i).getLink());
		     item.appendChild(itLink);
		     
		     Node guid=rss.createElement("guid");
		     guid.setTextContent(element.get(i).getLink());
		     item.appendChild(guid);
		     
		     /*Node date=rss.createElement("pubDate");
		     date.setTextContent(element.get(i).getCreation_date().toGMTString());
		     item.appendChild(date);*/
		     
		     Node itDes=rss.createElement("description");
		     itDes.setTextContent(element.get(i).getDescription());
		     item.appendChild(itDes);
		     
		}
        
		return rss;
	}
	
	 /**
     *Metodo che converte un documento in una stringa
     * @param x Documento da convertire
     * @return String
     */
    public String docToString(Document rss) {
        String xmlString = null;
        try {
            Transformer transformer = TransformerFactory.newInstance().newTransformer();
            transformer.setOutputProperty(OutputKeys.INDENT, "yes");

            //initialize StreamResult with File object to save to file
            StreamResult result = new StreamResult(new StringWriter());
            DOMSource source = new DOMSource(rss);
            transformer.transform(source, result);

            xmlString = result.getWriter().toString();

        } catch (TransformerConfigurationException ex) {
            System.err.println("Errore di configurazione del trasformatore; " + ex.getMessage());
        } catch (TransformerException ex) {
            //fai un listner
        } finally {
            return xmlString;
        }
    }

	}
